//
//  XNCommonBillViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/12.
//

#import "XNCommonBillViewController.h"
#import "XNBillApproveFlowRequest.h"
#import "XNCommonBillInfoRequest.h"
#import "XNCommonBillInfoTableViewCell.h"

@interface XNCommonBillViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNCommonBillInfoModel *infoModel;

@end

@implementation XNCommonBillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNCommonBillInfoRequest *infoRequest = [[XNCommonBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNCommonBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNCommonBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNBillApproveFlowRequest *approveRequest = [[XNBillApproveFlowRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNBillApproveFlowRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNBillApproveFlowRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}


- (void)creatTableData
{
    
    XNUserModel *payToUser = self.infoModel.createUserObject;
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单部门： %@",payToUser.departmentName],
                              [NSString stringWithFormat:@"职务： %@",payToUser.positionName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [self.infoModel.expandApplyList enumerateObjectsUsingBlock:^(XNExpandApplyModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.budgetInfoObject.remainReimburseMoney < 0) {
            itemModel.showOverImage = true;
            *stop = YES;
        }
    }];
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"报销申请单信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"报销方式（直接报销）: %@",self.infoModel.expendType ? @"否":@"是"],
                               [NSString stringWithFormat:@"申请人： %@",payToUser.userName],
                               [NSString stringWithFormat:@"岗位： %@",payToUser.positionName],
                               [NSString stringWithFormat:@"付款部门： %@",self.infoModel.payDepartName],
                               [NSString stringWithFormat:@"币别： %@",self.infoModel.currency],
                               [NSString stringWithFormat:@"付款方式： %@",self.infoModel.payment],
                               [NSString stringWithFormat:@"付款对象： %@",self.infoModel.supplierName],
                               [NSString stringWithFormat:@"报销月份： %@",self.infoModel.expendMonth],
                               [NSString stringWithFormat:@"附件张数合计： %ld",self.infoModel.appendixNum],
                               @"附件信息：",
                               nil];
    itemModel2.showItemArray = showItemArray2;
    itemModel2.fileArray = self.infoModel.fileList;
    itemModel2.fileIsImage = NO;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    

    XNTableShowDataModel *reimbursementModel = [[XNTableShowDataModel alloc] init];
    reimbursementModel.showType = XNShowType_Normal;
    reimbursementModel.title = @"报销明细";
    reimbursementModel.showWarning = YES;
    reimbursementModel.warningStr = @"填写的注意事项：\r1、摘要填写：填写简要的人、时间、费用类别，例如：XXX某个月的XXX费用，报销单上会打印出来；\r2、备注填写：详细报销事由及说明，或不适合打印在报销单上的说明内容报销单上不会打印出来。";
    reimbursementModel.headTitleArray = [NSArray arrayWithObjects:
                                         [NSString stringWithFormat:@"报销金额总计(含税)： %.3f",self.infoModel.expendAmountWithDuty],
                                         [NSString stringWithFormat:@"报销金额总计(不含税)： %.3f",self.infoModel.expendAmountWithOutDuty],
                                         [NSString stringWithFormat:@"描述： %@",self.infoModel.remark.length > 0 ? self.infoModel.remark : @""],
                                         nil];
    [self.infoModel.expandApplyList enumerateObjectsUsingBlock:^(XNExpandApplyModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3;
        if (self.infoModel.expendType) {
            NSString *billCode = [obj.applyId substringToIndex:2];
            NSString *creatDate = @"";
            NSString *applyer = @"";
            NSString *applyDate = @"";
            NSString *budgetName = @"";
            CGFloat totalPrice = 0;
            if ([billCode isEqualToString:@"FY"]) {
                creatDate = obj.expenseObject.createTimeStr;
                applyer = obj.expenseObject.createUserName;
                applyDate = obj.expenseObject.duringTypeValue;
                budgetName = obj.budgetInfoObject.budgetName;
                totalPrice = obj.expenseObject.totalCost;
            }else if([billCode isEqualToString:@"PX"]){
                creatDate = obj.educationObject.createTimeStr;
                applyer = obj.educationObject.createUserName;
                applyDate = [obj.educationObject.createTime substringToIndex:4];
                budgetName = obj.budgetInfoObject.budgetId;
                totalPrice = obj.educationObject.totalCost;
            }else if([billCode isEqualToString:@"WR"]){
                creatDate = obj.careConfirmObject.createTimeStr;
                applyer = obj.careConfirmObject.createUserName;
                applyDate = [obj.careConfirmObject.createTime substringToIndex:4];
                budgetName = obj.budgetInfoObject.budgetId;
                totalPrice = obj.careConfirmObject.totalCost;
            }else if([billCode isEqualToString:@"PY"]){
                creatDate = obj.payObject.createTimeStr;
                applyer = obj.payObject.createUserName;
                applyDate = [NSString stringWithFormat:@"%ld",(long)obj.payObject.applyYear];
                budgetName = obj.payObject.budgetItemName;
                totalPrice = obj.payObject.totalCost;
            }
            showItemArray3 = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"来源单号： %@",obj.applyId],
                              [NSString stringWithFormat:@"制单日期： %@",creatDate],
                              [NSString stringWithFormat:@"申请人： %@",applyer],
                              [NSString stringWithFormat:@"申请年份： %@",applyDate],
                              [NSString stringWithFormat:@"预算目录： %@",budgetName.length > 0 ? budgetName :@""],
                               [NSString stringWithFormat:@"本次报销金额(含税)： %@",obj.expendWithDuty],
                               [NSString stringWithFormat:@"本次报销金额(不含税)： %@",obj.expendWithOutDuty],
                               [NSString stringWithFormat:@"申请金额： %.3f",totalPrice],
                               [NSString stringWithFormat:@"发票类别： %@",obj.invoiceType],
                                [NSString stringWithFormat:@"税率： %@",obj.taxCode],
                               [NSString stringWithFormat:@"摘要： %@",obj.remark],
                               [NSString stringWithFormat:@"附件张数： %@",obj.appendixNum],
                               [NSString stringWithFormat:@"预算说明： %@",obj.budgetInfoObject.showStr],
                               nil];
        }else{
            showItemArray3 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"预算目录： %@",obj.budgetName.length > 0 ? obj.budgetName :@""],
                                       [NSString stringWithFormat:@"本次报销金额(含税)： %@",obj.expendWithDuty],
                                       [NSString stringWithFormat:@"本次报销金额(不含税)： %@",obj.expendWithOutDuty],
                                       [NSString stringWithFormat:@"申请年份： %@",obj.expendYear],
                                       [NSString stringWithFormat:@"发票类别： %@",obj.invoiceType],
                                        [NSString stringWithFormat:@"税率： %@",obj.taxCode],
                                       [NSString stringWithFormat:@"摘要： %@",obj.remark],
                                       [NSString stringWithFormat:@"附件张数： %@",obj.appendixNum],
                                       [NSString stringWithFormat:@"预算说明： %@",obj.budgetInfoObject.showStr],
                                       nil];
        }

        temp.showItemArray = showItemArray3;
        [reimbursementModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:reimbursementModel];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}



@end
